package com.wanmait.hr.controller;

import com.wanmait.hr.service.ConfigService;
import com.wanmait.hr.service.DepartmentService;
import com.wanmait.hr.service.PostService;
import com.wanmait.hr.service.PostTypeService;
import com.wanmait.hr.util.Pager;
import com.wanmait.hr.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * PostManagementController class
 *
 * @author 王志斌
 * @date 2019-02-01
 * */
@Controller
public class PostManagementController {
    @Resource
    private PostTypeService postTypeService;
    @Resource
    private PostService postService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ConfigService configService;

    /**岗位管理
     * @param model
     * */
    @RequestMapping("/manage/department/post_management/list")
    public void getList(Model model)
    {
        //获取所有的岗位类别，二级菜单
        List<PostType> postTypeList = postTypeService.findAllByPostType();
        //获取每页显示的信息数量
        Config config = configService.configInfoByKey("department/post_management/list");
        //显示所有的岗位_已注释layui自动加载
        //List<Post> postList = postService.getAllPost(0,Integer.parseInt(config.getValue()));
        //获取所有的一级类别
        List<PostType> postTypeOne = postTypeService.getAllPostType();
        //获取所有的部门信息
        List<Department> getAllDepartmentList = departmentService.getAllDepartmentList();
        //获取部门信息总条数
        Integer dataCount = postService.dataCount();

        model.addAttribute("postTypeList", postTypeList);
        //model.addAttribute("postList", postList);
        model.addAttribute("postTypeOne", postTypeOne);
        model.addAttribute("getAllDepartmentList", getAllDepartmentList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("config", config);
    }

    /**获取Ajax回调岗位管理分页
     * @param pageNo
     * @param pageSize
     * */
    @RequestMapping(value = "/manage/department/post_management/ajaxSuccessPage", method = RequestMethod.POST)
    public ModelAndView getPostList(Integer pageNo, Integer pageSize)
    {
        //获取部门信息总条数
        Integer dataCount = postService.dataCount();

        Pager pager = new Pager();
        pager.setPageNo(pageNo);
        pager.setPageSize(pageSize);
        pager.setDataCount(dataCount);

        //获取所有的岗位信息
        List<Post> postList = postService.getAllPost(pager);

        ModelAndView mv = new ModelAndView();
        mv.addObject("postListPage", postList);
        mv.addObject("type", "pager");
        mv.setViewName("/manage/department/post_management/ajaxSuccessList");
        return mv;
    }


    /**岗位管理Ajax回调
     * @param postTypeId
     * @param model
     * */
    @RequestMapping(value = "/manage/department/post_management/ajaxSuccessList", method = RequestMethod.POST)
    public void getPostAjaxSuccessList(Integer postTypeId, Model model)
    {
        //根据岗位类别的id查询岗位信息
        List<Post> postList = postService.postInfoById(postTypeId);

        model.addAttribute("postList", postList);
    }

    /**根据部门id查询岗位信息
     * @param departId
     * @return ModelAndView
     * */
    @RequestMapping(value = "/manage/department/post_management/ajaxSuccessDepart", method = RequestMethod.POST)
    public ModelAndView getPostAjaxDepart(Integer departId)
    {
        List<Post> postInfoByDepartId = postService.postInfoByDepartId(departId);

        ModelAndView mv = new ModelAndView();
        mv.addObject("postInfoByDepartId", postInfoByDepartId);
        mv.addObject("type", "departMentType");
        mv.setViewName("/manage/department/post_management/ajaxSuccessList");
        return mv;
    }

    /**岗位类别管理
     * @param model
     * */
    @RequestMapping("/manage/department/post_management/postType")
    public void getPostType(Model model)
    {
        //获取所有的岗位类别，二级菜单
        List<PostType> postTypeList = postTypeService.findAllByPostType();
        model.addAttribute("postTypeList", postTypeList);
    }

    /**新增岗位类别与岗位
     * @param model
     * @param type
     * */
    @RequestMapping("/manage/department/post_management/insert")
    public void getInsert(Model model, Integer type)
    {
        //新增岗位
        if(type == 1)
        {
            //新增岗位时获取所有的一级类别
            List<PostType> postTypeOne = postTypeService.getAllPostType();
            //新增岗位时获得所有的二级类别
            List<PostType> postTypeTwo = postTypeService.postTypeTwo();
            //获取所有的部门信息
            List<Department> getAllDepartmentList = departmentService.getAllDepartmentList();

            model.addAttribute("postTypeOne", postTypeOne);
            model.addAttribute("postTypeTwo", postTypeTwo);
            model.addAttribute("getAllDepartmentList", getAllDepartmentList);
        }

        //新增类别
        if(type == 2)
        {
            //新增类别时获取所有的一级类别
            List<PostType> postTypeList = postTypeService.getAllPostType();
            model.addAttribute("postTypeList", postTypeList);
        }

        model.addAttribute("type", type);
    }

    /**新增岗位类别
     * @param parentName
     * @param parentVal
     * @param session
     * @param type
     * @param post
     * */
    @RequestMapping(value = "/manage/department/post_management/insert", method = RequestMethod.POST)
    public void getPostInsert(String parentName, Integer parentVal, Integer type, HttpSession session, Post post)
    {
        //User user = (User)session.getAttribute("userinfo");

        User user = new User();
        user.setId(1);

        if(type == 2)
        {
            //新增岗位类别
            postTypeService.insertPostType(parentName, parentVal, user.getId());
        }

        if(type == 1)
        {
            //新增岗位
            post.setUploadAdmin(user);
            postService.insertPost(post);
        }

    }

    /**岗位类别删除
     * @param postTypeId
     * */
    @RequestMapping("/manage/department/post_management/Delete")
    public void getDelete(Integer postTypeId)
    {
        postTypeService.postTypeDelete(postTypeId);
    }

    /**岗位类别编辑
     * @param postType
     * @param type
     * @param model
     * */
    @RequestMapping(value = "/manage/department/post_management/update", method = RequestMethod.GET)
    public void getUpdate(PostType postType, Integer type, Model model)
    {
        //修改一级类别
        if(type == 2)
        {
            model.addAttribute("postTypeInfo", postType);
        }

        //修改二级类别
        if(type == 3)
        {
            //修改上级类别时获取所有的一级类别
            List<PostType> postTypeList = postTypeService.getAllPostType();
            model.addAttribute("postTypeList", postTypeList);
            model.addAttribute("postTypeInfo", postType);
        }

        model.addAttribute("type", type);
    }

    /**岗位类别编辑
     * @param type
     * @param postType
     * @param model
     * @param session
     * @param post
     * */
    @RequestMapping(value = "/manage/department/post_management/update", method = RequestMethod.POST)
    public void getPostUpdate(Integer type, PostType postType, Model model, HttpSession session, Post post)
    {
        //User user = (User)session.getAttribute("userinfo");
        User user = new User();
        user.setId(1);

        //修改一级类别
        if(type == 2)
        {
            postType.setUpdateAdmin(user);
            postTypeService.postTypeUpdate(postType);
        }

        //修改二级类别
        if(type == 3)
        {
            postType.setUpdateAdmin(user);
            postTypeService.postTypeUpdate(postType);
        }

    }

    /**岗位编辑
     * @param model
     * @param postId
     * */
    @RequestMapping("/manage/department/post_management/updatePost")
    public void getUpdatePost(Model model, Integer postId)
    {
        //修改岗位时获取所有的一级类别
        List<PostType> postTypeOne = postTypeService.getAllPostType();
        //修改岗位时获得所有的二级类别
        List<PostType> postTypeTwo = postTypeService.postTypeTwo();
        //获取所有的部门信息
        List<Department> getAllDepartmentList = departmentService.getAllDepartmentList();
        //根据id获取要修改的岗位信息
        Post post = postService.postInfo(postId);

        model.addAttribute("postTypeOne", postTypeOne);
        model.addAttribute("postTypeTwo", postTypeTwo);
        model.addAttribute("getAllDepartmentList", getAllDepartmentList);
        model.addAttribute("postInfo", post);
    }

    /**岗位编辑
     * @param post
     * */
    @RequestMapping(value = "/manage/department/post_management/updatePost", method = RequestMethod.POST)
    public void getPostUpdatePost(Post post)
    {
        postService.updatePost(post);
    }

    /**岗位删除
     * @param postId
     * */
    @RequestMapping(value = "/manage/department/post_management/delPost", method = RequestMethod.POST)
    public void getDelPost(Integer postId)
    {
        postService.delPost(postId);
    }







}
